package com.atguigu.tingshu.common.delay;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
//这段代码是一个使用Redisson实现的延迟消息发送服务。
@Slf4j
@Component
public class DelayMsgService {
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 发送延迟消息
     * @param queueName
     * @param msg
     * @param daley
     */

    public void sendDelayMessage(String queueName,String msg,Long daley){
        try {
            //创建阻塞对象
            RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(queueName);
            //创建延迟队列
            RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
            //发送消息
            delayedQueue.offer(msg,daley,TimeUnit.SECONDS);
            log.info("发送延迟消息成功：{}", msg);
        } catch (Exception e) {
            log.error("发送延迟消息失败：{}", msg);
            throw new RuntimeException(e);
        }

    }
}
